//
//  PageManager.m
//  Menu
//
//  Created by trimad on 11-11-17.
//  Copyright 2011 Trimad Group. All rights reserved.
//

#import "DisplayManager.h"

#import "Category.h"
#import "DataService.h"
#import "DisplayService.h"
#import "Item.h"
#import "Page.h"
#import "PageDisplay.h"
#import "PageLayoutView.h"

@interface DisplayManager(Private)

- (NSArray *)populateTopCategories;
- (NSArray *)populatePages;

@end

@implementation DisplayManager

@synthesize topCategories;
@synthesize pages;

static DisplayManager *_instance = nil;
DataService *dataService = nil;
DisplayService *displayService = nil;
NSMutableDictionary *itemDic = nil;
NSMutableDictionary *itemCategoryDic = nil;
NSMutableDictionary *itemPageDic = nil;
NSMutableDictionary *categoryStartPageDic = nil;
NSMutableDictionary *categoryEndPageDic = nil;

+ (DisplayManager *)instance {
	if (!_instance) {
		@synchronized(self) {
			if (!_instance) {
				_instance = [[DisplayManager alloc] init];
			}
		}
	}
	return _instance;
}

- (id)init {
	self = [super init];
	itemDic = [[NSMutableDictionary alloc] init];
	itemCategoryDic = [[NSMutableDictionary alloc] init];
	itemPageDic = [[NSMutableDictionary alloc] init];
	categoryStartPageDic = [[NSMutableDictionary alloc] init];
	categoryEndPageDic = [[NSMutableDictionary alloc] init];
	dataService = [DataService instance];
	displayService = [DisplayService instance];
	topCategories = [self populateTopCategories];
	pages = [self populatePages];
	return self;
}

- (NSArray *)populateTopCategories {
	NSArray *topCategoryArray = dataService.topCategories;
	for (int i = 0; i < topCategoryArray.count; i++) {
		Category *topCategory = [topCategoryArray objectAtIndex:i];
		NSArray *subCategoryArray = topCategory.subCategories;
		for (int j = 0; j < subCategoryArray.count; j++) {
			Category *subCategory = [subCategoryArray objectAtIndex:j];
			NSArray *itemArray = subCategory.items;
			for (int k = 0; k < itemArray.count; k++) {
				Item *item = [itemArray objectAtIndex:k];
				[itemDic setObject:item forKey:item.code];
				[itemCategoryDic setObject:subCategory forKey:item.code];
			}
		}
	}
	
	return topCategoryArray;
}

- (NSArray *)populatePages {
	NSMutableArray *pageArray = [[NSMutableArray alloc] init];
	NSArray *pageDisplays = displayService.pages;
	NSString *tempTopCategoryCode = nil;
	NSString *tempSubCategoryCode = nil;
	Page *previous = nil;
	for (int i = 0; i < pageDisplays.count; i++) {
		Page *page = [[Page alloc] init];
		PageDisplay *pd = [pageDisplays objectAtIndex:i];
		page.number = i;
		page.viewType = pd.viewType;
		NSMutableArray *pageItems = [[NSMutableArray alloc] initWithCapacity:10];
		NSArray *itemCodes = pd.itemCodes;
		for (int j = 0; j < itemCodes.count; j++) {
			NSString *itemCode = [itemCodes objectAtIndex:j];
			Item *item = [itemDic objectForKey:itemCode];
			[pageItems addObject:item];
			if (j == 0) {
				page.subCategory = [itemCategoryDic objectForKey:itemCode];
				page.topCategory = page.subCategory.parentCategory;
			}
			[itemPageDic setObject:page forKey:itemCode];
		}
		page.items = pageItems;
		[pageArray addObject:page];
		NSString *code = page.topCategory.code;
		if (![code isEqualToString:tempTopCategoryCode]) {
			[categoryStartPageDic setObject:page forKey:code];
			if (tempTopCategoryCode != nil) {
				[categoryEndPageDic setObject:previous forKey:tempTopCategoryCode];
			}
			tempTopCategoryCode = code;
		}
		code = page.subCategory.code;
		if (![code isEqualToString:tempSubCategoryCode]) {
			[categoryStartPageDic setObject:page forKey:code];
			if (tempSubCategoryCode != nil) {
				[categoryEndPageDic setObject:previous forKey:tempSubCategoryCode];
			}
			tempSubCategoryCode = code;
		}
		previous = page;
	}
	if (tempTopCategoryCode != nil) {
		[categoryEndPageDic setObject:previous forKey:tempTopCategoryCode];
	}
	if (tempSubCategoryCode != nil) {
		[categoryEndPageDic setObject:previous forKey:tempSubCategoryCode];
	}
	return pageArray;
}

- (Page *)page:(int)number {
	if (number >= [pages count]) {
		return nil;
	}
	return [pages objectAtIndex:number];
}

- (int)pageIndex:(NSString *)itemCode {
	Page *page = [itemPageDic objectForKey:itemCode];
	return page.number;
}

- (int)pageStart:(NSString *)categoryCode {
	Page *page = [categoryStartPageDic objectForKey:categoryCode];
	return page.number;
}

- (int)pageEnd:(NSString *)categoryCode {
	Page *page = [categoryEndPageDic objectForKey:categoryCode];
	return page.number;
}

- (NSArray *)searchItems:(NSString *)keyword {
	NSMutableArray *items = [[NSMutableArray alloc] init];
	NSArray *allItems = [itemDic allValues];
	for (int i = 0; i < allItems.count; i++) {
		Item *item = [allItems objectAtIndex:i];
		if ([item.name rangeOfString:keyword].length > 0) {
			[items addObject:item];
		}
	}
	return items;
}

- (void)dealloc {
	[topCategories release];
	[pages release];
	[dataService release];
	[displayService release];
	[itemDic release];
	[itemCategoryDic release];
	[itemPageDic release];
	[categoryStartPageDic release];
	[categoryEndPageDic release];
	[super dealloc];
}

@end
